import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Rectangle;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

/**
 * @author Azure
 * @version 1.0 31/03/07
 */
public class AzWindowsScrollFrame extends JFrame implements AdjustmentListener {
    JScrollBar vScrollBar = new JScrollBar();

    JScrollBar hScrollBar = new JScrollBar();

    JTable table;
    int i=0;

    public AzWindowsScrollFrame() {
        init();
    }

    public static void main(String[] args) {
        try {
            UIManager
                    .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {
            System.err.println("Something went wrong!");
        }
        AzWindowsScrollFrame sf = new AzWindowsScrollFrame();
        sf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        sf.setLocationRelativeTo(null);
        sf.setSize(new Dimension(300, 200));
        sf.setVisible(true);
    }

    private void init() {
        this.setLayout(new BorderLayout());

        Object data[][] = { { "one", "1", "swing" }, { "two", "2", "is" },
                { "three", "3", "very" }, { "four", "4", "nice" },
                { "five", "5", "swing" }, { "six", "6", "is" },
                { "seven", "7", "very" }, { "eight", "8", "nice" },
                { "nine", "9", "swing" }, { "ten", "10", "is" } };
        Object headers[] = { "col1", "col2", "col3" };
        table = new JTable(data, headers);
        table.setPreferredSize(new Dimension(500, 300));

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane
                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        scrollPane
                .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0));
        panel.setBorder(new EmptyBorder(0, 0, 0, 0));
        panel.add(scrollPane, BorderLayout.CENTER);

        JPanel hPanel = new JPanel(new BorderLayout());

        hScrollBar.setOrientation(JScrollBar.HORIZONTAL);
        int visibleAmount = 100;
        hScrollBar.setVisibleAmount(visibleAmount);
        hPanel.add(hScrollBar, BorderLayout.CENTER);

        JLabel vConerlabel = new JLabel();
        vConerlabel.setMinimumSize(new Dimension(15, 10));
        vConerlabel.setPreferredSize(new Dimension(15, 10));
        vConerlabel.setBorder(new EmptyBorder(0, 0, 0, 0));
        hPanel.add(vConerlabel, BorderLayout.EAST);

        vScrollBar.addAdjustmentListener(this);
        int hVisibleAmount = 60;
        vScrollBar.setVisibleAmount(hVisibleAmount);
        vScrollBar.setUnitIncrement(60);
        vScrollBar.setValueIsAdjusting(true);

        vScrollBar.addMouseWheelListener(new MouseWheelListener() {
            public void mouseWheelMoved(MouseWheelEvent e) {
                vScrollBar.setValue(vScrollBar.getValue()
                        + e.getWheelRotation() * 60);
            }
        });
        hScrollBar.addAdjustmentListener(this);
        hScrollBar.addMouseWheelListener(new MouseWheelListener() {
            public void mouseWheelMoved(MouseWheelEvent e) {
                hScrollBar.setValue(hScrollBar.getValue()
                        + e.getWheelRotation() * 60);
            }
        });

        this.add(hPanel, BorderLayout.SOUTH);
        this.add(panel, BorderLayout.CENTER);
        this.add(vScrollBar, BorderLayout.EAST);
    }

    public void adjustmentValueChanged(AdjustmentEvent e) {
        if (e.getSource() == vScrollBar) {
//            System.out.println(e.getAdjustmentType());
            System.out.println("X:" + vScrollBar.getX());
            System.out.println("Y:"+vScrollBar.getY());;

        } else if (e.getSource() == hScrollBar) {
            scrollToTableH(table, e.getValue());
        }
    }

    public void scrollToTableH(JTable table, int value) {
        if (!(table.getParent() instanceof JViewport)) {
            return;
        }
        JViewport viewport = (JViewport) table.getParent();
        Rectangle viewRect = viewport.getViewRect();
        Rectangle rect = new Rectangle(value * table.getWidth() / 90,
                (int) viewRect.getY(), 10, 10);

        rect.setLocation(rect.x - viewRect.x, rect.y - viewRect.y);
        viewport.scrollRectToVisible(rect);
    }

    public void scrollToTableV(JTable table, int value) {
        if (!(table.getParent() instanceof JViewport)) {
            return;
        }
        JViewport viewport = (JViewport) table.getParent();
        Rectangle viewRect = viewport.getViewRect();
        Rectangle rect = new Rectangle((int) viewRect.getX(), value
                * table.getHeight() / 90, 10, 10);

        rect.setLocation(rect.x - viewRect.x, rect.y - viewRect.y);
        viewport.scrollRectToVisible(rect);
    }

}